<template>
  <div class="home">
    <!-- 主页头部结构 -->
    <el-container>
      <el-header>
        <div class="header_left">
          <img src="../assets/heima.png" alt="">
          <h1>xibJane的后台管理系统</h1>
        </div>
        <el-button class="outBtn" @click="outLogin">退出</el-button>
      </el-header>
      <el-container>
        <!-- 主页左侧分类结构 -->
        <el-aside :width="collapseBl? '64px':'200px'">
          <!-- 折叠导航 -->
          <div class="toggleMenu" @click="collapse">|||</div>
          <!-- 用户管理 -->
          <el-menu
            :default-active="activeDefault"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="rgb(64, 158, 255)"
            unique-opened
            :collapse="collapseBl"
            :collapse-transition="false"
            router>
            <el-submenu v-for="item in menus" :key="item.id" :index="item.id + ''">
              <template slot="title">
                <i :class="iconsList[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="'/' + item2.path">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{item2.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主页主体部分结构 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      collapseBl: false,
      activeDefault: ''
    }
  },

  created () {
    this.getMenu()
    this.activeDefault = this.$route.path
  },

  methods: {
    // 点击退出清空token返回到login页面
    outLogin () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenu () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menus = res.data
    },
    collapse () {
      this.collapseBl = !this.collapseBl
    }
  }
}
</script>

<style scoped lang='less'>
  .home,.el-container {
    height: 100%;
  }
  // 头部样式
  .el-header{
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #373d41;

    .header_left {
      display: flex;
      align-items: center;

      h1 {
        margin-left: 10px;
        font-size: 25px;
        font-weight: 400;
        color: #fff;
      }
    }
  }
  // 左侧分类样式
  .el-aside{
    background: #373d41;

    .toggleMenu {
      width: 100%;
      height: 40px;
      line-height: 40px;
      text-align: center;
      color: #fff;
      font-size: 16px;
      background: #666;
      letter-spacing: 5px;
      cursor: pointer;
      user-select: none;
    }

    .el-menu{
      border-right:none;

      span {
        margin-left: 10px;
      }
    }
  }
  // 主体样式
  .el-main{
    background: #eaedf1;
  }
</style>
